/**
 * 
 */
package l1j.opqlo.假人系統;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.logging.Level;
import java.util.logging.Logger;

import javolution.util.FastMap;
import javolution.util.FastTable;
import l1j.opqlo.NewSystem.A_ColorMessage;
import l1j.server.DatabaseFactory;
import l1j.server.server.model.Instance.L1NpcInstance;
import l1j.server.server.utils.SQLUtil;

/**
 * @author user
 *
 */
public class A_DummyChatName
{
	private static final Logger log = Logger.getLogger(A_DummyChatName.class.getName());
	private static final FastMap<Integer, FastTable<Chat>> _chatList = new FastMap<Integer, FastTable<Chat>>();
	private static int index;
	
	private A_DummyChatName()
	{
	}
	
	public static void load()
	{
		Connection con = null;
		PreparedStatement pstm = null;
		ResultSet rs = null;
		
		try
		{
			con = DatabaseFactory.get().getConnection();
			pstm = con.prepareStatement("SELECT * FROM z_npc_talk");
			rs = pstm.executeQuery();
			
			while (rs.next())
			{
				int npcid = rs.getInt("npcid");
				Chat chat = new Chat();
				chat.setNpcId(npcid);
				chat.setChatType(rs.getByte("chat_type"));
				chat.setChatContent(rs.getString("chat_content"));
				chat.setChatTime(rs.getInt("chat_time") * 1000);
				
				if (_chatList.containsKey(npcid))
				{
					_chatList.get(npcid).add(chat);
				}
				else
				{
					FastTable<Chat> fastTable = new FastTable<Chat>();
					fastTable.add(chat);
					_chatList.put(npcid, fastTable);
				}
			}
		}
		catch (SQLException e)
		{
			log.log(Level.SEVERE, e.getLocalizedMessage(), e);
		}
		finally
		{
			SQLUtil.close(rs, pstm, con);
		}
	}
	
	public static final void nextIndex()
	{
		index++;
	}
	
	public static final void startDummyChat(L1NpcInstance npc)
	{
		FastTable<Chat> chatTable = _chatList.get(npc.getNpcId());
		
		if (chatTable == null)
		{
			return;
		}
		
		Chat c = chatTable.get(index % chatTable.size());
		long currentTime = System.currentTimeMillis();
		
		if (currentTime > c.getLastTime())
		{
			c.setLastTime(currentTime + c.getChatTime());
			String content = c.getChatContent();
			byte type = c.getChatType();
			npc.broadcastPacketAll(new A_ColorMessage(npc, type, content));
		}
	}
	
	private static class Chat
	{
		private int npcid;
		private byte chatType;
		private String chatContent;
		private int chatTime;
		private long lastTime;
		
		/**
		 * @return chatContent
		 */
		public final String getChatContent()
		{
			return chatContent;
		}
		
		/**
		 * @param chatContent 的設定的 chatContent
		 */
		public final void setChatContent(String chatContent)
		{
			this.chatContent = chatContent;
		}
		
		/**
		 * @return chatTime
		 */
		public final int getChatTime()
		{
			return chatTime;
		}
		
		/**
		 * @param chatTime 的設定的 chatTime
		 */
		public final void setChatTime(int chatTime)
		{
			this.chatTime = chatTime;
		}
		
		/**
		 * @return chatType
		 */
		public final byte getChatType()
		{
			return chatType;
		}
		
		/**
		 * @param chatType 的設定的 chatType
		 */
		public final void setChatType(byte chatType)
		{
			this.chatType = chatType;
		}
		
		/**
		 * @param npcid 的設定的 npcid
		 */
		public final void setNpcId(int npcid)
		{
			this.npcid = npcid;
		}

		/**
		 * @return lastTime
		 */
		public final long getLastTime()
		{
			return lastTime;
		}

		/**
		 * @param lastTime 的設定的 lastTime
		 */
		public final void setLastTime(long lastTime)
		{
			this.lastTime = lastTime;
		}
	}
}
